<template>
  <div class="app-container">
    <!-- 筛选区域 -->
    <el-form :model="filter" class="filter-container" inline>
      <el-form-item label="用户名" prop="username">
        <el-input v-model="filter.username" placeholder="请输入用户名" clearable />
      </el-form-item>
      
      <el-form-item label="用户角色" prop="role">
        <el-select v-model="filter.role" placeholder="请选择角色" clearable>
          <el-option
            v-for="item in roleOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      
      <el-form-item class="filter-actions">
        <el-button @click="resetFilter">重置</el-button>
        <el-button type="primary" @click="search">搜索</el-button>
      </el-form-item>
    </el-form>

    <el-table
      :data="list"
      style="width: 100%"
      border
      fit
      highlight-current-row
      :header-cell-style="{ background: '#f5f7fa', fontWeight: 'bold' }"
    >
      <el-table-column prop="id" label="ID" width="80" align="center" />
      <el-table-column prop="username" label="用户名" width="auto" align="center" />
      <el-table-column label="角色" width="auto" align="center">
        <template #default="scope">
          <el-tag :class="'role-tag role-' + scope.row.role" effect="dark">
            {{ scope.row.role === 'admin' ? '管理员' : '普通用户' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="collection" label="收藏" width="auto" align="center" />
      <el-table-column prop="comment" label="评论" width="auto" align="center" />
      <el-table-column prop="time" label="时间" width="auto" align="center" />
      <el-table-column label="操作" width="220" align="center">
        <template #default="scope">
          <div class="action-buttons">
            <el-button type="primary" size="mini" @click="handleView(scope.row)">查看</el-button>
            <el-button type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>
            <el-button type="warning" size="mini" @click="handleResetPwd(scope.row)">改密</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      background
      layout="total, prev, pager, next"
      :total="totalCount"
      :page-size="pageSize"
      :current-page="currentPage"
      @current-change="handlePageChange"
      class="pagination-container"
    />
  </div>
</template>

<script>
export default {
  data() {
    return {
      list: [],
      totalCount: 50,
      pageSize: 10,
      currentPage: 1,
      filter: {
        username: '',
        role: ''
      },
      roleOptions: [
        { value: 'admin', label: '管理员' },
        { value: 'user', label: '普通用户' }
      ]
    }
  },
  created() {
    this.generateMockData()
  },
  methods: {
    search() {
      // 执行搜索逻辑
      this.generateMockData()
    },
    resetFilter() {
      this.filter = {
        username: '',
        role: ''
      }
      this.generateMockData()
    },
    generateMockData() {
      // 生成20条模拟数据
      const mockData = []
      for (let i = 1; i <= 6; i++) {
        mockData.push({
          id: i,
          username: `用户${i}`,
          role: ['admin','user'][Math.floor(Math.random() * 2)], 
          collection: Math.floor(Math.random() * 1000),
          comment: Math.floor(Math.random() * 100),
          time: `2024-02-${i.toString().padStart(2, '0')} 10:00`
        })
      }
      this.list = mockData.filter(item => {
          const nameMatch = item.username.includes(this.filter.username)
          const roleMatch = this.filter.role ? item.role === this.filter.role : true
          return nameMatch && roleMatch
        })
    },
    handleView(row) {
      console.log('查看', row)
    },
    handleDelete(row) {
      console.log('删除', row)
    },
    handleResetPwd(row) {
      console.log('修改密码', row)
    },
    handlePageChange(page) {
      this.currentPage = page
    }
  }
}
</script>

<style scoped>
.app-container {
  padding: 20px;
}

.filter-container {
  padding: 20px;
  background: #f8f9fa;
  border-radius: 4px;
  margin-bottom: 20px;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.05);
}

.filter-actions {
  float: right;
  margin-right: 20px;
}

.el-form-item {
  margin-bottom: 0;
  margin-right: 15px;
}

.pagination-container {
  margin-top: 30px;
  display: flex;
  justify-content: center;
}

.action-buttons {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
 .el-button+.el-button { 
    margin-left: 0px; 
 }
/* 修复的样式部分 */
:deep(.el-table th) {
  /* 表头样式 */
  background-color: #f5f7fa!important;
}

:deep(.el-table__row) {
  height: 60px;
}

:deep(.el-table__header) {
  background-color: #f5f7fa !important;
  color: #606266;
  border-radius: 4px 4px 0 0;
}

.role-tag {
  margin: 2px 5px;
  font-weight: bold;
  min-width: 70px;
  text-align: center;
  border-radius: 12px;
}
.role-tag.role-admin {
  background-color: #ecf5ff;
  color: #409eff;
  border-color: #d9ecff;
}
.role-tag.role-user {
  background-color: #f0f9eb;
  color: #67c23a;
  border-color: #e1f3d8;
}
</style>